package next.CommonProfile;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;

import next.Functionality.S_3R4_Activity;
import next.TemplateActivity.R;
import next.view.support.Products_ExpandableListAdapter;
import next.view.support.S_3R4_Object;
import next.view.support.S_3R4_ObjectType;


public class Products extends S_3R4_Activity {
	
	 //--Variables
	 private Products_ExpandableListAdapter adapter;
	 private ExpandableListView listView;
	 private Button voiceBTN, BarCodeBTN, CodeBTN, ViewCart;
	 private String contenido="";
	 private String filter;
	 //--Taking resources
	 private ArrayList<S_3R4_Object> catalog, cart;
	 private String[] menuItems = {"ok","sorry"};
	 private S_3R4_Object selectedContextMenuProduct; 
		
		@Override
		 public void onCreate(Bundle savedInstanceState) {
		        super.onCreate(savedInstanceState);
		        setContentView(R.layout.app_common_list_products_layout);
		        		        
		        //--Taking resources
		       /* catalog = ShoppingCartHelper.getCatalog(getResources());
		        cart=ShoppingCartHelper.getCart();*/
		        
		        //--Assembling XML widgets
		        voiceBTN=(Button)findViewById(R.id.app_common_list_products_layoutBN1);
		        BarCodeBTN=(Button)findViewById(R.id.app_common_list_products_layoutBN2);
		        CodeBTN=(Button)findViewById(R.id.app_common_list_products_layoutBN3);
		        ViewCart=(Button) findViewById(R.id.app_common_list_products_layoutBN4);
		      
		        fromOrder();
		        
		        BarCodeBTN.setOnClickListener(new View.OnClickListener(){

			        public void onClick(View v) {
			
			        	Intent intent = new Intent("com.google.zxing.client.android.SCAN");
		                intent.setPackage("com.google.zxing.client.android");
		                intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
		                intent.putExtra("SCAN_MODE", "1D_BARCODE");
		                startActivityForResult(intent, 0);
			
			        }

		        });
		        
		        voiceBTN.setOnClickListener(new View.OnClickListener(){

			        public void onClick(View v) {
			
			        	/*Intent i = new Intent(ProductsActivity.this, VoiceRecognitionDemo.class);
			        	  	startActivityForResult(i,1);*/
			        	
			        }

		        });
		        
		        CodeBTN.setOnClickListener(new View.OnClickListener(){

			        public void onClick(View v) {
			
			        	/*adapter.activateCODE();
			        	if (adapter.isCodeActivate())
			        		CodeBTN.setText("Is-Activate");
			        	else
			        		CodeBTN.setText("Is-Not-Activate");*/
			        }

		        });
		        
		        ViewCart.setOnClickListener(new View.OnClickListener(){

			        public void onClick(View v) {
			
			           /* Intent viewShoppingCartIntent = new Intent(getBaseContext(), ShoppingCartActivity.class);
		        	    startActivity(viewShoppingCartIntent);*/
			        }

		        });

		 }		
	
		public void onActivityResult(int requestCode, int resultCode, Intent intent) {
	        if (requestCode == 0) {
	            if (resultCode == RESULT_OK) {
	                String contents = intent.getStringExtra("SCAN_RESULT");
	                String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
	                // Handle successful scan
	                contenido = contents;
	                //It does not exist!
	                
	        		S_3R4_Object p=new S_3R4_Object("vuala", "vuala23", 23, 78);
	        		//p=S_3R4_Object.RetrieveProduct(Long.valueOf(contenido));
	        		//System.out.println("name P: "+p.getName());
	        		if(p.getA()!=0)
	        		{
	        			filter=p.getChain1();
	        			adapter.getFilter().filter(filter);
	        		}
	        		//else
	        			//Dialogs.AlertDialog(this, "Product not found", "Acept");
	                
	            } else if (resultCode == RESULT_CANCELED) {
	            	adapter.getFilter().filter("");
	            }
	        }else if (requestCode == 1)
	        {
	        	if (resultCode == RESULT_OK){
	        	  String word = intent.getStringExtra("SelectedWord");
	        	  filter=word;
	        	  adapter.getFilter().filter(filter);
	        	}else if (resultCode == RESULT_CANCELED) {
	        		adapter.getFilter().filter("");
	            }
	        	
	        }
	    }
		
	public void fromOrder()
	{
		  
       // Retrive the ExpandableListView from the layout
       listView = (ExpandableListView) findViewById(R.id.app_common_list_products_layoutELV1);
       
       // Initialize the adapter with blank groups and children
        adapter = new Products_ExpandableListAdapter(this, new ArrayList<S_3R4_ObjectType>(), new ArrayList<ArrayList<S_3R4_Object>>()); 
        
      //  ArrayList<S_3R4_Object> pts= Refs.refs.getPts();
       for(int i=0; i<10; i++){
     		for(int j=0; j<4; j++){
     			adapter.addItem( new S_3R4_Object("chain"+i, "chain2"+i, i, i+1000),
     					new S_3R4_ObjectType("type"+i, "type2"+i, i+2000, i+10000));
      		}
        }
       
       listView.setOnChildClickListener(new OnChildClickListener()
       {
           
          // @Override
           public boolean onChildClick(ExpandableListView arg0, View arg1, int arg2, int arg3, long arg4)
           {
           	  /*Intent i = new Intent(ProductsActivity.thisprivate TitleBar titleBar;, ItemOfProductByCatalogActivity.class);
     	        b.putLong("IdProductoEscogido", adapter.getChildId(arg2, arg3) );
               i.putExtras(b);
     	     startActivity(i);*/
           	 
        	  /* S_3R4_Object p=(S_3R4_Object) adapter.getChild(arg2, arg3);
		        ShoppingCartHelper.addCart(p);       
		        ShoppingCartHelper.setSelectedProductID(p.getId());
		        Intent viewShoppingCartIntent = new Intent(getBaseContext(), ShoppingCartActivity.class);
       	    startActivity(viewShoppingCartIntent);
         	/*   Toast.makeText(getBaseContext(), getBaseContext()+"", Toast.LENGTH_SHORT).show();*/
         	return false;
           }
       });
       
       listView.setOnGroupClickListener(new OnGroupClickListener()
       {
           
           //@Overrideprivate TitleBar titleBar;
           public boolean onGroupClick(ExpandableListView arg0, View arg1, int arg2, long arg3)
           {
               /*Toast.makeText(getBaseContext(), "Group clicked", Toast.LENGTH_SHORT).show();*/
               return false;
           }
       });
          // Set this blank adapter to the list view
          listView.setAdapter(adapter);
          listView.setTextFilterEnabled(true);
          registerForContextMenu(listView);
          
	}
	
		
		
	
}